package pikes.peak;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.junit.Test;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Inline;
import pikes.html.xhtml.PCData;
import pikes.peak.test.AssertHtml;
import pikes.peak.test.PikesPeakComponentTestCase;
import pikes.peak.test.PikesPeakTestRequest;

public class TextTestCase extends PikesPeakComponentTestCase {

	@Test
	public void simpleText() throws Exception {
		Text text = new Text("Some text");

		Inline actual = text.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(new PCData("Translated Text"), actual);
	}

	@Test
	public void simpleTextWithEmptyParameters() throws Exception {
		Text text = new Text("Some text", new TextParameters() {

			@Override
			protected void addParameters(List<Object> parameters, Map model, RequestContext requestContext)
					throws Exception {
			}

		});

		Inline actual = text.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(new PCData("Translated Text"), actual);
	}

	@Test
	public void simpleTextWithOneParameter() throws Exception {
		StaticMessageSource newMessageSource = new StaticMessageSource();
		newMessageSource.addMessage("Parameter Text Key", Locale.ENGLISH, "Parameter Text {0}");

		Text text = new Text("Parameter Text Key", new TextParameters() {

			@Override
			protected void addParameters(List<Object> parameters, Map model, RequestContext requestContext)
					throws Exception {
				parameters.add("boo");
			}

		});

		PikesPeakTestRequest pikesPeakRequest = new PikesPeakTestRequest(newMessageSource, themeMessageSource,
				localeResolver, themeResolver);

		Inline actual = text.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(new PCData("Parameter Text boo"), actual);
	}

	@Test
	public void simpleTextWithProvider() throws Exception {
		Text text = new Text(new RequestProperty<CharSequence>() {

			public CharSequence getValue(Map model, RequestContext requestContext) throws Exception {
				return "Some text";
			}

		});

		Inline actual = text.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(new PCData("Some text"), actual);
	}

	@Test
	public void simpleText1() throws Exception {
		Text text = new Text("Some text1");

		Inline actual = text.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(new PCData("Some text1"), actual);
	}

	@Test
	public void simpleTextLookupInThemeMessageSources() throws Exception {
		StaticMessageSource newMessageSource = new StaticMessageSource();
		Text text = new Text("Some text");

		PikesPeakTestRequest pikesPeakRequest = new PikesPeakTestRequest(newMessageSource, themeMessageSource,
				localeResolver, themeResolver);

		Inline actual = text.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(new PCData("Translated Theme Text"), actual);
	}
}
